package royalpay

import (
    "fmt"
	"time"
	"errors"
	"thirdpaysdk/core"
	"thirdpaysdk/utils/signature"
	//"github.com/astaxie/beego"
)

type QueryOrderResponse struct {
	ReturnCode     string  `json:"return_code"`
	ReturnMsg      string  `json:"return_msg,omitempty"`
	ResultCode     string  `json:"result_code"`
	OrderId        string  `json:"order_id"`
	PartnerOrderId string  `json:"partner_order_id"`
	TotalFee       int     `json:"total_fee"`
	RealFee        int     `json:"real_fee"`
	Rate           float64 `json:"rate"`
	PayTime        string  `json:"pay_time"`
	CreateTime     string  `json:"create_time"`
	Currency       string  `json:"currency"`
	Channel        string  `json:"channel"`
}

func QueryOrder(partnerCode,orderId,credentialCode string)(rsp *QueryOrderResponse, err error){
	incompleteURL := "https://mpay.royalpay.com.au/api/v1.0/gateway/partners/%s/orders/%s?time=%d&nonce_str=%s&sign=%s"
	
	rsp = &QueryOrderResponse{}
	curTime := time.Now().Unix()*1000
	nonceStr := signature.GetNonceStr(20)
	sign := signature.MakeSha256Sign(partnerCode,curTime,nonceStr,credentialCode)
	serverUrl := fmt.Sprintf(incompleteURL, partnerCode, orderId, curTime, nonceStr, sign)
	//
	client := core.GetPayJSONClient("GET", serverUrl)
	tbdata, e := client.PayJsonExcute(nil, rsp)
    if e != nil {
		err = e
		return
	}
	if rsp.ReturnCode != "SUCCESS" {
		err = errors.New(string(tbdata))
        return
	}
	return
}
